package com.leo.jiami;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Base64;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;

import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;

import static android.R.attr.id;
import static android.R.attr.key;
import static android.icu.lang.UCharacter.JoiningGroup.E;

public class MainActivity extends AppCompatActivity {

    private String mEncryptResult=null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private EditText input,pw;
    private TextView show;
    private void initView() {
        pw= (EditText) findViewById(R.id.pw);
        input= (EditText) findViewById(R.id.inputEncrypt);
        show= (TextView) findViewById(R.id.show);

    }

    public void encrypt(View view) {


        String data = input.getText().toString().trim();
        String pwd=pw.getText().toString().trim();


        switch (view.getId()){

            case R.id.desEncrypt://Des加密
                //需要类型，要加密的内容，需要密码
                mEncryptResult=  encrypt("DES",data,pwd);
                show.setText(mEncryptResult);
                break;
            case R.id.desDecrypt:
                    mEncryptResult=  decrypt("DES",mEncryptResult,pwd);
                show.setText(mEncryptResult);

                break;
            case R.id.desedeEncrypt:

                mEncryptResult=  encrypt("DESed",data,pwd);
                show.setText(mEncryptResult);
                break;
            case R.id.desedeDecrypt:
                break;



            case R.id.aesedeEncrypt:
                mEncryptResult=  encrypt("DES",data,pwd);
                show.setText("AES加密的结果是："+mEncryptResult);
              break;
            case R.id.aesDecrypt:
                mEncryptResult=  decrypt("DES",data,pwd);
                show.setText("AES解密的结果是："+mEncryptResult);
                break;

        }

    }

    private String decrypt(String type, String data, String pwd) {
        switch (type){
            case "DES":
                try {
                    //获取加密对应的类，加密引擎
                    Cipher cipher=Cipher.getInstance(type);
                    //对加密引擎进行初始化，指定密码
                    if (pwd.getBytes().length!=8){

                        throw new Exception("密码8位");

                    }
                    Key key=new SecretKeySpec(pwd.getBytes(),type);
                    cipher.init(Cipher.DECRYPT_MODE,key);

                    ///执行加密，
                    if (!TextUtils.isEmpty(data)) {
                        byte[] decode = Base64.decode(data.getBytes(),Base64.DEFAULT);
                        byte[] bytes = cipher.doFinal(decode);
                        return new String(bytes);
                    }
                } catch (NoSuchAlgorithmException e) {
                    e.printStackTrace();
                } catch (NoSuchPaddingException e) {
                    e.printStackTrace();
                } catch (InvalidKeyException e) {
                    e.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;



        }
        return null;
    }

    private String encrypt(String type, String data, String pwd) {
        switch (type){

            case "DES":
                try {
                    //获取加密对应的类，加密引擎
                    Cipher cipher=Cipher.getInstance(type);
                    //对加密引擎进行初始化，指定密码
                    if (pwd.getBytes().length!=8){

                        throw new Exception("密码8位");

                    }
                    Key key=new SecretKeySpec(pwd.getBytes(),type);
                    cipher.init(Cipher.ENCRYPT_MODE,key);

                    ///执行加密，
                    if (!TextUtils.isEmpty(data)) {
                        byte[] bytes = cipher.doFinal(data.getBytes());
                        byte[] encode = Base64.encode(bytes,Base64.DEFAULT);
                        return new String(encode);
                    }
                } catch (NoSuchAlgorithmException e) {
                    e.printStackTrace();
                } catch (NoSuchPaddingException e) {
                    e.printStackTrace();
                } catch (InvalidKeyException e) {
                    e.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case "DESed":

                break;
            case "AES":
                break;
            case "DeAES":
                break;
            default:








        }


        return null;
    }
}
